package model.item;

import model.entity.BasicEntity;
import model.entity.Entity;
import utilities.*;

public class InteractiveItem extends Item implements Archiveable {

	private String activatedName;
	private boolean activated;
	private Condition conditions;
	
	public InteractiveItem()
	{
		activatedName = "default";
		activated = false;
		conditions = null;
	}
	
	public InteractiveItem(String name, String activatedName, Condition conditions)
	{
		super(name);
		this.activatedName = activatedName;
		this.activated = false;
		this.conditions = conditions;
	}
	
	public boolean isActivated() {
		return activated;
	}

	public void setActivated(boolean activated) {
		this.activated = activated;
	}
	
	public String getActivatedName() {
		return activatedName;
	}

	public void setActivatedName(String activatedName) {
		this.activatedName = activatedName;
	}
	
	public void touch(BasicEntity entity)
	{
		Entity actor = (Entity) entity;
		if(conditions.meetsRequirements(actor))
			this.activated = true;
	}
	
	public Archive save() {
		Archive archive = super.save();
		
		archive.putAttribute("activated", Boolean.toString(this.activated));
		
		archive.putAttribute("activatedName", this.activatedName);
		
		archive.putClassName("conditions", this.conditions.getClass().getName());
		archive.putOneRelationship("conditions", this.conditions);
		
		return archive;
	}
	
	public void load(Archive archive) {
		super.load(archive);
		
		this.activated = Boolean.parseBoolean(archive.getAttribute("activated"));
		
		this.activatedName = archive.getAttribute("activatedName");
		
		this.conditions = (Condition)ModelFactory.getInstance().createInstanceWithClassName(archive.getClassName("conditions"));
		this.conditions.load(archive.getOneRelationship("conditions"));
	}
}
